//-----------------------------------------------------------------------
// <copyright file="HistoryDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BirthdayReminder.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace BirthdayReminder.Data
{    
    public abstract class HistoryDAOBase
    {
        #region Common methods
        public virtual History CreateHistoryFromReader(IDataReader reader)
        {
            History item = new History();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("HistoryId"))) item.HistoryId = (int)reader["HistoryId"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReceiverId"))) item.ReceiverId = (int)reader["ReceiverId"];
                if (!reader.IsDBNull(reader.GetOrdinal("SenderId"))) item.SenderId = (int)reader["SenderId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Message"))) item.Message = (string)reader["Message"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreationDate"))) item.CreationDate = (DateTime)reader["CreationDate"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateHistoryFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateHistory methods
            
        public virtual void CreateHistory(History history)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHistoriesInsert");
                
                database.AddInParameter(dbCommand, "@ReceiverId", DbType.Int32, history.ReceiverId);
                database.AddInParameter(dbCommand, "@SenderId", DbType.Int32, history.SenderId);
                database.AddInParameter(dbCommand, "@Message", DbType.String, history.Message);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, history.CreationDate);
                database.AddOutParameter(dbCommand, "@HistoryId", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                history.HistoryId = (int)database.GetParameterValue(dbCommand, "@HistoryId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateHistoryException, ex);
            }
        }

        #endregion
		
        #region UpdateHistory methods
        
        public virtual void UpdateHistory(History history)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHistoriesUpdate");            
                
                database.AddInParameter(dbCommand, "@HistoryId", DbType.Int32, history.HistoryId);
                database.AddInParameter(dbCommand, "@ReceiverId", DbType.Int32, history.ReceiverId);
                database.AddInParameter(dbCommand, "@SenderId", DbType.Int32, history.SenderId);
                database.AddInParameter(dbCommand, "@Message", DbType.String, history.Message);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, history.CreationDate);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateHistoryException, ex);
            }
        }
        
        #endregion
		
        #region DeleteHistory methods
        public virtual void DeleteHistory(int historyId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHistoriesDelete");
                
                database.AddInParameter(dbCommand, "@HistoryId", DbType.Int32, historyId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteHistoryException, ex);
            }
        }

        #endregion
		
        #region GetHistory methods
        
        public virtual History GetHistory(int historyId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHistoriesGet");
                
                database.AddInParameter(dbCommand, "@HistoryId", DbType.Int32, historyId);
                
                History history =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        history = CreateHistoryFromReader(reader);
                        reader.Close();
                    }
                }
                return history;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetHistoryException, ex);
            }
        }
        
        #endregion
		
        #region GetHistoryList methods
        public virtual HistoryCollection GetHistoryList(HistoryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHistoriesGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HistoryCollection historyCollection = new HistoryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        History history = CreateHistoryFromReader(reader);
                        historyCollection.Add(history);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return historyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetHistoryListException, ex);
            }
        }
        
        public virtual HistoryCollection GetHistoryList(HistoryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetHistoryList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}